//
//  ShouCangViewController.m
//  Zaker
//
//  Created by lirui on 16/3/17.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ShouCangViewController.h"
#import "CoreDataManager.h"
#import "Video.h"
#import "SeeVideoDetailViewController.h"
#import "UIScrollView+EmptyDataSet.h"
@interface ShouCangViewController ()<UITableViewDataSource,UITableViewDelegate,DZNEmptyDataSetDelegate,DZNEmptyDataSetSource>
@property(nonatomic,retain)UITableView *tableView;
@property(nonatomic,retain)NSArray *arr;
@property(nonatomic, retain)NSMutableArray *dataArr;
@property(nonatomic ,retain)CoreDataManager *manager;
@property(nonatomic ,retain)SeeModel *model;
@end

@implementation ShouCangViewController

-(void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden  = YES;
    
}
-(void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden =  NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self creatTableView];
//    self.dataArr = [ NSMutableArray array];
     self.manager =  [CoreDataManager defaults];
    NSLog(@"url == %@" , [self.manager applicationDocumentsDirectory]);
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Video"];
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES];
    [request setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    NSError *error;
    _arr = [self.manager.managedObjectContext executeFetchRequest:request error:&error];
      [self data];
}
-(void)data
{
    self.dataArr = [_arr mutableCopy];
    [_tableView reloadData];
}
-(void)creatTableView
{
    self.tableView = [[ UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH * 375, HEIGHT * 667) style:UITableViewStylePlain];
    self.tableView.dataSource= self;
    self.tableView.delegate = self;
    self.tableView.emptyDataSetDelegate = self;
    self.tableView.emptyDataSetSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tableView];
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

        return _dataArr.count;
   
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
 
   
    SeeModel *m = _dataArr[indexPath.row];
    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.textLabel.text =  m.title;
   
    return cell;
    
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SeeVideoDetailViewController *seeVideo = [[ SeeVideoDetailViewController alloc] init];
    seeVideo.sModel  = _dataArr[indexPath.row];
    [self.navigationController pushViewController:seeVideo animated:YES];
    
}

//编辑方法
-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{   //如果没有父类方法 状态无法反转
    [super setEditing:editing animated:animated];
    //使用编辑按钮 控制tableview的编辑状态
    [_tableView setEditing:editing animated:animated];
    
    
    
}
//是否允许编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return YES;
}
#pragma mark -编辑样式
-(UITableViewCellEditingStyle )tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return UITableViewCellEditingStyleDelete;
    
}
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}
//通过编辑状态判断编辑操作
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    //通过参数editingstyle判断 执行不同操作
    //删除操作
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [self.manager.managedObjectContext deleteObject:_dataArr[indexPath.row]];
        [self.manager saveContext];
        NSArray *arr = [NSArray arrayWithObject:indexPath];
        [_dataArr removeObjectAtIndex:indexPath.row];
        [_tableView deleteRowsAtIndexPaths:arr withRowAnimation:UITableViewRowAnimationFade];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"changeLike" object:nil];
        
    }
    
   }

#pragma mark - 当数据为空时显示的字符
-(NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
{
    NSString *text = @"你还没有收藏内容哦,快快去收藏~";
    NSDictionary *str = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:16.0f]};
    return [[NSAttributedString alloc] initWithString:text attributes:str];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
